package com.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

/**
 * @author 陈昆昆
 * @version 1.0
 * @date 2020/4/8 20:08
 */
public class HelloWord {
    public static void main(String[] args) {
        //1.读取配置文件，初始化工厂对象
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        //2.获取SecurityManager实例
        SecurityManager manager = factory.getInstance();
        //3.将SecurityManager绑定到工具类
        SecurityUtils.setSecurityManager(manager);
        //4.通过SecurityUtils得到当前登录的用户
        Subject currentUser = SecurityUtils.getSubject();
        //5.窗口登录令牌
        UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
        try {
            //6.登录并传入令牌
            currentUser.login(token);
            System.out.println("身份信息验证成功！");
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            System.out.println("用户名错误！");
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.out.println("用户名密码错误！");
        }
        //7.退出
        currentUser.logout();

    }
}
